$(document).ready(function () {
    init();
})

function init() {
    getBookList();
    // 绑定搜索按钮点击事件
    const searchButton = document.getElementById("search");
    searchButton.addEventListener("click", function () {
        searchBooks();
    });
}

function getBookList() {
    // 发送AJAX请求获取图书列表
    $.ajax({
        url: "BookLIstServlet",
        type: "GET",
        dataType: "json",
        success: function (data) {
            addBookList(data);
            console.log(data);
        },
        error: function (xhr, status, error) {
            // 处理发生错误时的情况
            console.log("Error: " + error);
        }
    });
}

function searchBooks() {
    event.preventDefault()
    // 获取搜索关键字
    const number = document.getElementById("number").value;
    const name = document.getElementById("name").value;
    const author = document.getElementById("author").value;
    const publisher = document.getElementById("publisher").value;


    // 发送AJAX请求
    $.ajax({
        url: "BookLIstServlet",
        type: "GET",
        data: {
            number: number,
            name: name,
            author: author,
            publisher: publisher,

        },
        dataType: "json",
        success: function (data) {
            addBookList(data);
        },
        error: function (xhr, status, error) {
            console.log("Error: " + error);
        }
    });
}

function addBookList(data) {
    const tbody = document.getElementById("bookList");
    let html = "";
    for (let i = 0; i < data.length; i++) {
        const book = data[i];
        html += `
            <tr>
                <td>${book.number}</td>
                <td>${book.name}</td>
                <td>${book.author}</td>
                <td>${book.publisher}</td>
                <td>${book.price}</td>
                <td>${book.quantity}</td>
                <td>${book.isBorrow ? "是" : "否"}</td>
                <td>${book.isSell ? "是" : "否"}</td>
                <td>
                    <button onclick="borrow(${book.number})">借出</button>
                    <button onclick="sell(${book.number})">购买</button>
                </td>
            </tr>`;
    }
    tbody.innerHTML = html;
}

function borrow(number) {
    $.ajax({
        url: 'ClientBorrowServlet',
        type: 'POST',
        data: {
            number: number
        },
        success: function (data) {
            if (data.msg === "success") {
                alert("借出成功");
            } else if (data.msg === "fall") {
                alert("已借出或售出，无法借出");
            }
            getBookList(); // 在请求成功后刷新页面
        },
        error: function (xhr, status, error) {
            console.error('借出失败：' + number + '，错误信息：' + error);
        }
    });
}

function sell(number) {
    $.ajax({
        url: 'ClientSellServlet',
        type: 'POST',
        data: {
            number: number
        },
        success: function (data) {
            console.log(data)
            if (data.msg === "success") {
                alert("购买成功");
            } else if (data.msg === "fall") {
                alert("已卖出，无法购买");
            }
            getBookList(); // 在请求成功后刷新页面
        },
        error: function (xhr, status, error) {
            alert("购买失败");
            console.error('购买失败：' + number + '，错误信息：' + error);
        }
    });
}

